package bl.accountbook;

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import po.AccountBookPo;
import po.Po;
import po.SystemLogPo;
import bl.client.ClientBL;
import bl.commodity.CommodityBL;
import bl.commodity.CommodityTypeBL;
import bl.finance.AccountorBL;
import blservice.accountbook.AccountBookBLService;
import blservice.client.ClientBLService;
import blservice.commodity.CommodityBLService;
import blservice.commodity.CommodityTypeBLService;
import blservice.finance.AccountorBLService;
import vo.AccountBookVo;
import vo.AccountorVo;
import vo.ClientVo;
import vo.CommodityTypeVo;
import vo.CommodityVo;
import vo.Vo;
import db.Database;
import db.client.ClientDB;
import db.commodity.CommodityDB;
import db.commodity.CommodityTypeDB;
import db.finance.AccountorDB;
import dbservice.accountbook.AccountBookDBService;
import dbservice.history.SystemLogDBService;

public class AccountBookBL extends UnicastRemoteObject implements AccountBookBLService{

	private static final long serialVersionUID = 7390501330801897036L;
	AccountBookDBService db;
	SystemLogDBService log;
	
	
	private AccountBookPo ABV2P(AccountBookVo vo){
		if(vo==null){
			return Database.getDefaultAB();
		}
		AccountBookPo po=new AccountBookPo();
		po.setName(vo.getName());
		List<Object> info=new ArrayList<Object>();
		info.add(vo.get商品信息());
		info.add(vo.get客户信息());
		info.add(vo.get银行账户信息());
		info.add(vo.get商品分类信息());
		po.setInfo(info);
		return po;
	}
	
	@SuppressWarnings("unchecked")
	private AccountBookVo ABP2V(AccountBookPo po){
		AccountBookVo vo=new AccountBookVo();
		if(po.isSuccessed()){
			vo.setName(po.getName());
			if(po.getInfo()!=null){
				vo.set商品信息((List<CommodityVo>) po.getInfo().get(0));
				vo.set客户信息((List<ClientVo>) po.getInfo().get(1));
				vo.set银行账户信息((List<AccountorVo>) po.getInfo().get(2));
				vo.set商品分类信息((List<CommodityTypeVo>) po.getInfo().get(3));
			}
			vo.successed();
		}else{
			vo.failed(po.getFailMessage());
		}
		return vo;
	}
	
	public AccountBookBL(AccountBookDBService abds,SystemLogDBService log)throws RemoteException {
		db = abds;
		this.log=log;
	}

	@Override
	public Vo add(AccountBookVo account) throws RemoteException, MalformedURLException, NotBoundException, SQLException {
		Vo r=new Vo();
		Po po=db.add(ABV2P(account));
		if(po.isSuccessed()){
			r.successed();
			log.add(new SystemLogPo(account.getOperator(), "增加账目", account.getName()));
			CommodityBLService commodityBl=new CommodityBL(new CommodityDB(ABV2P(account)),log);
			ClientBLService clientBl=new ClientBL(new ClientDB(ABV2P(account)),log);
			AccountorBLService accountorBl=new AccountorBL(new AccountorDB(ABV2P(account)),log);
			CommodityTypeBLService commodityTypeBl=new CommodityTypeBL(new CommodityTypeDB(ABV2P(account)), log);
			
			for(CommodityTypeVo vo:account.get商品分类信息()){
				commodityTypeBl.addCommodityType(vo);
			}
			/* 商品分类的添加要在商品添加之前 */
			for(CommodityVo vo:account.get商品信息()){
				commodityBl.addCommodity(vo);
			}
			for(ClientVo vo:account.get客户信息()){
				clientBl.addClient(vo);
			}
			for(AccountorVo vo:account.get银行账户信息()){
				accountorBl.addAccountor(vo);
			}
		}else{
			r.failed(po.getFailMessage());
		}
		return r;
	}

	@Override
	public AccountBookVo get(AccountBookVo account) throws RemoteException {
		AccountBookPo po=db.get(ABV2P(account));		
		return ABP2V(po);
	}

	@Override
	public AccountBookVo getDefaultAccountBook() throws RemoteException {
		AccountBookPo po=db.getDefaultAccountBook();		
		return ABP2V(po);
	}

	@Override
	public List<AccountBookVo> getAll() throws RemoteException {
		List<AccountBookVo> result=new ArrayList<AccountBookVo>();
		List<AccountBookPo> po=db.getAll();
		for(AccountBookPo o:po){
			result.add(ABP2V(o));
		}
		return result;
	}
}
